package org.s170.servlet.HouTaiServlet;



import org.s170.dao.AdmintratoDao;
import org.s170.entity.Admintrao;
import org.s170.servlet.BaseServlet;
import org.s170.utils.JwtUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/login")
public class AdminLoginServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setContentType("text/html,charset=utf-8");
       req.setCharacterEncoding("utf-8");
        String account=req.getParameter("username");
        String passsword=req.getParameter("password");

        if(!"".equals(account) && !"".equals(passsword)){
            AdmintratoDao dao=new AdmintratoDao();
            Admintrao admintrato=dao.queyUser(account,passsword);
            if (admintrato!=null){
                Map<String,Object> map=new HashMap<>();
                map.put("username",account);
                String token= JwtUtil.createToken(map,30*60*1000);
                print(resp,successJson(token));
                return;
            }
        }

        print(resp,errorJson(401,"错误的账号或密码"));
    }
}
